package com.levtech.moviemanager.loan;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.levtech.moviemanager.R;
import com.levtech.moviemanager.MovieManagerApp;
import com.levtech.moviemanager.db.DataStore;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;

public class ViewLoanActivity extends Activity
{
	private static final String TITLE = "Title";
	private static final String CONTENT = "Content";
	private DataStore store;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_view_loan);
		this.store = ((MovieManagerApp)this.getApplication()).getStore();
		populateList();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.view_loan, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem selection)
	{
		switch(selection.getItemId())
		{
			case R.id.edit_loan:	//startActivity(new Intent(this, EditLoanActivity.class));
									return true;
									
			case R.id.add_loan:	startActivity(new Intent(this, AddLoanActivity.class));
									return true;
			
			default: return super.onOptionsItemSelected(selection);
		}
	}
	
	private void populateList()
	{
		Loan[] list = this.store.selectLoan();
		//TODO figure out how to implement expandable list view
		SimpleExpandableListAdapter content = new SimpleExpandableListAdapter(this, 
												getGroupInfo(list), 
												android.R.layout.simple_expandable_list_item_1,
												new String[] { TITLE },
												new int[] { android.R.id.text1, android.R.id.text2 },
												getChildInfo(list),
												android.R.layout.simple_expandable_list_item_2,
												new String[] { TITLE, CONTENT },
												new int[] { android.R.id.text1, android.R.id.text2 });
		((ExpandableListView)findViewById(R.id.loanListview)).setAdapter(content);
	}
	
	private List<HashMap<String, String>> getGroupInfo(Loan[] lList)
	{
		ArrayList<HashMap<String, String>> groups = new ArrayList<HashMap<String, String>>();
		
		for(Loan l : lList)
		{
			HashMap<String, String> temp = new HashMap<String, String>();
			temp.put(TITLE, l.getMovie().getTitle());
			groups.add(temp);
		}
		
		return groups;
	}
	
	private List<List<HashMap<String, String>>> getChildInfo(Loan[] lList)
	{
		List<List<HashMap<String, String>>> children = new ArrayList<List<HashMap<String, String>>>();
		
		for(Loan l : lList)
		{
			ArrayList<HashMap<String, String>> child = new ArrayList<HashMap<String, String>>();
			HashMap<String, String> temp = new HashMap<String, String>();
			temp.put(CONTENT, l.toString());
			child.add(temp);
			children.add(child);
		}
		
		return children;
	}

}
